package com.scm.biz.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scm.biz.bean.SysRole;
import com.scm.biz.query.OrderQuery;
import com.scm.biz.service.OrderService;
import com.scm.core.bean.resp.RespBean;

/**
 * 显示所有订单
 * @author 张乐锋
 *
 */
@Controller
@RequestMapping("/order")
public class OrderAction {
	
	@Resource
	private OrderService orderService;
	
	/**
	 * 展示所有
	 * @param allOrderQuery
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(OrderQuery orderQuery){
		if(orderQuery.getPageNo()==0){
			orderQuery.setPageNo(1);
		}
		Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());
		orderQuery.setPerformer(userId);
		ModelAndView mv = new ModelAndView("order/order_list");
		List<SysRole> sysRoleList = (List<SysRole>)SecurityUtils.getSubject().getSession().getAttribute("_roleList");
		//判断是否为设计总监或者供应链总监
		for(SysRole sysRole:sysRoleList){
			if(sysRole.getCode().equals("R007") || sysRole.getCode().equals("R001")){
				orderQuery.setIsOpen(1);
				break;
			}
		}
		System.out.println(orderQuery.getIsOpen()+"##########################");
		mv.addObject("orders", orderService.getAllOrder(orderQuery));
		mv.addObject("splitPage", orderQuery.getSplitPage());
		mv.addObject("code", orderQuery.getCode());
		mv.addObject("customerName", orderQuery.getCustomerName());
		mv.addObject("orderType", orderQuery.getOrderType());
		
		
		
//		List<AllOrderResult> allOrderResults = OrderService.getAllOrder(allOrderQuery);
//		List<AllOrderDto> allOrderDtos = new ArrayList<AllOrderDto>();
//		for(AllOrderResult allOrderResult : allOrderResults){
//			AllOrderDto allOrderDto = new AllOrderDto();
//			allOrderDto.setId(allOrderResult.getId());
//			SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
//			allOrderDto.setCreateAt(fmt.format(allOrderResult.getCreateAt()));
//			allOrderDto.setCode(allOrderResult.getCode());
//			if(allOrderResult.getRemainingTime()!=null){
//				allOrderDto.setRemainingTime(allOrderResult.getRemainingTime());
//			}
//			allOrderDto.setFullname(allOrderResult.getFullname());
//			allOrderDto.setAbbreviation(allOrderResult.getAbbreviation());
//			allOrderDto.setWeb(allOrderResult.getWeb());
//			allOrderDto.setTargetConsumer(allOrderResult.getTargetConsumer());
//			allOrderDto.setCount(allOrderResult.getCount());
//			allOrderDto.setLevel(allOrderResult.getLevel());
//			allOrderDto.setAttachment(allOrderResult.getAttachment());
//			allOrderDto.setProductCategory(allOrderResult.getProductCategory());
//			allOrderDto.setStyleLabel(allOrderResult.getStyleLabel());
//			List<RoleNameResult> roleNameResults = new ArrayList<RoleNameResult>();
//			for(RoleNameResult rnr : allOrderResult.getParticipants()){
//				if(rnr.getRoleName().equals("销售")||rnr.getRoleName().equals("项目经理")||rnr.getRoleName().equals("设计师")){
//					roleNameResults.add(rnr);
//				}
//			}
//			allOrderDto.setParticipants(roleNameResults);
//			allOrderDto.setFactories(allOrderResult.getFactories());
//			if(allOrderResult.getStatus()==0){
//				allOrderDto.setOrderStatus("已关闭");
//			}
//			if(allOrderResult.getStatus()==1){
//				allOrderDto.setOrderStatus("设计中");
//			}
//			if(allOrderResult.getStatus()==2){
//				allOrderDto.setOrderStatus("打样中");
//			}
//			if(allOrderResult.getStatus()==3){
//				allOrderDto.setOrderStatus("大货中");
//			}
//			allOrderDtos.add(allOrderDto);
//		}
//		mv.addObject("allOrderDtos",allOrderDtos);
//		allOrderQuery.setRecourdCount(OrderService.getTotal(allOrderQuery));
//		mv.addObject("splitPage", allOrderQuery.getSplitPage());
//		mv.addObject("url", Const.getPath(req));
//		mv.addObject("name", allOrderQuery.getName());
//		mv.addObject("time", allOrderQuery.getTime());
//		mv.addObject("orderCode", allOrderQuery.getOrderCode());
//		mv.addObject("factoryName", allOrderQuery.getFactoryName());
//		mv.addObject("saleName", allOrderQuery.getSaleName());
//		mv.addObject("PMName", allOrderQuery.getPMName());
//		mv.addObject("designerName", allOrderQuery.getDesignerName());
		return mv;
	}
	@RequestMapping("/assignManager")
	@ResponseBody
	public RespBean<String> assignManager(Long businessIds,Integer manager,Long proofingOrderId)throws Exception{

		RespBean<String> respBean = new RespBean<>();
		if(proofingOrderId != null){
			orderService.saveAssignManager(businessIds, manager,proofingOrderId);
			
		}else{
			orderService.saveAssignManager(businessIds, manager);
			
		}
		return respBean;
	}
}
